package com.cloud.logger.zchain.zchainclient.interceptor;

/**
 * User: wcy
 * Date: 2017/4/8
 * Time: 16:24
 */
//public class AngelaLoggerInterceptor implements HandlerInterceptor {
//
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//        //重写request 读取流不会出错
//        WrapperRequest request = new WrapperRequest(httpServletRequest);
//
//        try{
//            long beginTime = System.currentTimeMillis();//1、开始时间
//
//            LoggerThreadLocal.setLoggerStartNodeTime(beginTime);
//            AngelaLoggerEntity logger = new AngelaLoggerEntity();
//            logger.setMethod(httpServletRequest.getMethod());
//            logger.setSourceIP(getIpAddress(httpServletRequest));
//            logger.setInterfaceIp(httpServletRequest.getRequestURL()+"");
//            logger.setInterfaceName(httpServletRequest.getRequestURI());
//            logger.setParms(readJSONString(request));
//
//            logger.setTenentId(httpServletRequest.getParameter("tenentId"));
//            LoggerThreadLocal.setLoggerId(AngelaAopLogger.loggerInterceptor(logger));
//        }catch (Exception ex){
//            System.out.println("获取参数失败！！");
//        }
//        return true;
//    }
//
//    public String readJSONString(HttpServletRequest request){
//        StringBuffer json = new StringBuffer();
//        String line = null;
//        try {
//            BufferedReader reader = request.getReader();
//            while((line = reader.readLine()) != null) {
//                json.append(line);
//            }
//        }
//        catch(Exception e) {
//            System.out.println(e.toString());
//        }
//        return json.toString();
//    }
//
//    public static String getIpAddress(HttpServletRequest request) {
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_CLIENT_IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        return ip;
//    }
//
//    public static Integer getRemotePort(final HttpServletRequest request){
//        try{
//            String port = request.getHeader("remote-port");
//            if(Strings.isNullOrEmpty(port)) {
//                try{
//                    return Integer.parseInt(port);
//                }catch(NumberFormatException ex){
//                    return 0;
//                }
//            }else{
//                return 0;
//            }
//        }catch(Exception e){
//            return 0;
//        }
//    }
//
//    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        long startTime = LoggerThreadLocal.getLoggerStartNodeTime();//开始时间
//        long endTime = System.currentTimeMillis();//结束时间
//        long elapsedTime = endTime -startTime;
//        System.out.println(elapsedTime);
//        AngelaLoggerEntity logger = new AngelaLoggerEntity();
//        logger.setElapsedTime(elapsedTime);
//        WrapperResponse wrapperResponse = new WrapperResponse((HttpServletResponse) httpServletResponse);
//        byte[] content = wrapperResponse.getContent();
//        logger.setResult(new String(content,"utf-8"));
//        AngelaAopLogger.updateLoggerInterceptor(LoggerThreadLocal.getLoggerId(), logger);
//    }

//}
